/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uit.qlks.baocao;

import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Restart
 */
public class BaoCao {

    private JFileChooser fc;
    private static String currentPath = null;
    public boolean  baocao( JTable jt, String html, String footer, String pright)
    {
        try
        {
//            UIDefaults defaultt = UIManager.getDefaults();
//            JOptionPane.showMessageDialog(null, UIManager.getLookAndFeel().toString());
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");//set giao dien
//            UIManager.setLookAndFeel("");

            fc = new JFileChooser(currentPath);
            fc.setAcceptAllFileFilterUsed(false);
            FileNameExtensionFilter filter = new FileNameExtensionFilter( "(*.html)", "html");
            fc.setFileFilter(filter);
            if(fc.showSaveDialog(null)== JFileChooser.CANCEL_OPTION)
            {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");//set giao dien
                return false;
            }

             //tao file moi
            File file = new File(fc.getSelectedFile() + ".html" );
            if(!file.createNewFile())//neu file da ton tai
            {
                if(JOptionPane.showConfirmDialog(null, "File đã tồn tại. Bạn có muốn ghi đè không?", "Warning!", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION)
                {
                    currentPath = file.getCanonicalPath();
                    html= MakeReport(jt,html,footer,pright);
                    PrintWriter out;
                    out = new PrintWriter(file);
                    out.println(html);
                    out.close();
                    JOptionPane.showMessageDialog(null,"Xuất file thành công!","Information",JOptionPane.INFORMATION_MESSAGE);
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");//set giao dien
                    return true;
                }
            }
            else
            {
                currentPath = file.getCanonicalPath();
                html= MakeReport(jt,html,footer, pright);
                PrintWriter out;
                out = new PrintWriter(file);
                out.println(html);
                out.close();
                JOptionPane.showMessageDialog(null,"Xuất file thành công!","Information",JOptionPane.INFORMATION_MESSAGE);
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");//set giao dien
                return true;
            }
            return true;
        }
        catch (Exception e)
        {

            return false;
        }

    }

    public String MakeReport(JTable jtb, String html1, String footer, String pright){
        try
        {
        int cols = jtb.getColumnCount();
        int rows = jtb.getRowCount();
        Date todayD=new Date(System.currentTimeMillis());
        SimpleDateFormat dayFormat= new SimpleDateFormat("dd-M-yyyy");
        String today =dayFormat.format(todayD.getTime());
        String html = "<meta http-equiv=Content-Type content='text/html; charset=utf8'>";
        html += "<br><LEFT>KHÁCH SẠN THÀNH PHỐ HỒ CHÍ MINH";
        html += "<p align=\"left\" style=\"margin-left : 35\">"+"----------"+today+"-----------</LEFT></p>";

        html += html1;
//        if(rows == 1)
            html+="<table border=\"1\" >";
        html+="<tr>";
        for (int i = 1; i <= cols; i++){
                html+="<th>"+jtb.getColumnName(i-1)+"</th>";
        }
        html+="</tr>";
        for(int i =1; i <= rows; i++){
                html+="<tr>";
                for (int j =1; j <= cols; j++){
                        html+="<td>"+jtb.getValueAt(i-1,j-1)+"</td>";
                }
                html+="</tr>";
        }
        html +="</table></center>";
        html += " <p align=\"right\" style=\"margin-right : "+pright+"\">" + footer;
        return html;
        }
        catch(Exception e)
        {
            return "";
        }
    }
}
